package com.ruoyi.radar.controller;

import com.ruoyi.coal.common.utils.R;
import com.ruoyi.coal.modules.coalpile.controller.CutUpController;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.CutUpEntity;
import com.ruoyi.coal.modules.coalpile.domain.PointEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CutUpVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.radar.domain.RadarSilo;
import com.ruoyi.radar.domain.RadarSiloResult;
import com.ruoyi.radar.domain.RadarSplit;
import com.ruoyi.radar.domain.RadarSplitSurface;
import com.ruoyi.radar.service.RadarSiloResultService;
import com.ruoyi.radar.service.RadarSplitService;
import com.ruoyi.radar.service.RadarSplitSurfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/radarSplit")
public class RadarSplitController extends BaseController {

    @Autowired
    private RadarSplitService radarSplitService;

    @Autowired
    private CutUpController cutUpController;

    @Autowired
    private RadarSplitSurfaceService radarSplitSurfaceService;

    @GetMapping("/list")
    public AjaxResult list(@RequestParam Map<String, Object> param)
    {
        Map<String, Object> result = new HashMap<>(2);
        List<RadarSplit> list = radarSplitService.selectSiloSplit(param);
        Integer count = radarSplitService.getCount(param);
        R cutList = cutUpController.list(param);
        result.put("data", list);
        result.put("coalData",cutList.get("page"));
        result.put("total", count);
        return AjaxResult.success(result);
    }

    @PostMapping
    public AjaxResult add(@RequestBody RadarSplit radarSplit)
    {
        try{
//            radarSplit.setSplitId(uuId());
            radarSplit.setCreateTime(currentTime());
            boolean save = radarSplitService.save(radarSplit);
            RadarSplitSurface radarSplitSurface = new RadarSplitSurface();
            radarSplitSurface.setId(radarSplit.getSplitId());
            radarSplitSurface.setStockid(radarSplit.getStockId());
            radarSplitSurface.setSurface(radarSplit.getSplitSurface());
            radarSplitSurfaceService.save(radarSplitSurface);
//            String[] splitStart = radarSplit.getSplitStart().split(",");
//            String[] splitEnd = radarSplit.getSplitEnd().split(",");

            CutUpVo cutUpVo = new CutUpVo();
            cutUpVo.setCutUpId(radarSplit.getSplitId());
            cutUpVo.setCoalYardId(radarSplit.getStockId());
            cutUpVo.setMeasureType(0);
            cutUpVo.setCoalPileDensity(Double.parseDouble(radarSplit.getCoalPileDensity()));
            cutUpVo.setCoalPileGround(Double.parseDouble(radarSplit.getCoalPileGround()));

            // 12    32
            // 14    34

//            xa1
//:
//{x: -57.668, y: 1.1151500000000003, z: -6.654}
//xa2
//:
//{x: 12.868, y: 1.1151500000000003, z: -6.654}
//xb1
//:
//{x: -57.668, y: 3.7124000000000006, z: -6.654}
//xb2
//:
//{x: 12.868, y: 3.7124000000000006, z: -6.654}
//ya1
//:
//{x: -24.9268, y: -12.521, z: -6.654}
//ya2
//:
//{x: -24.9268, y: 24.794, z: -6.654}
//yb1
//:
//{x: -13.303519999999999, y: -12.521, z: -6.654}
//yb2
//:
//{x: -13.303519999999999, y: 24.794, z: -6.654}

//            cutUpVo.setXa1(new PointEntity().set);
//                    this.pointService.save(cutUp.getXa1());
//        this.pointService.save(cutUp.getXa2());
//        this.pointService.save(cutUp.getXb1());
//        this.pointService.save(cutUp.getXb2());
//        this.pointService.save(cutUp.getYa1());
//        this.pointService.save(cutUp.getYa2());
//        this.pointService.save(cutUp.getYb1());
//        this.pointService.save(cutUp.getYb2());
            return toAjax(save);
        }catch (Exception e){
            return AjaxResult.success("操作失败");
        }
    }

    /**
     *修改料仓
     */
    @PreAuthorize("@ss.hasPermi('radarSilo:split:update')")
    @Log(title = "修改料仓", businessType = BusinessType.UPDATE)
    @PutMapping
//    @Transactional
    public AjaxResult edit(@Validated @RequestBody RadarSplit radarSplit)
    {
        try{
            boolean b = radarSplitService.updateById(radarSplit);
            CutUpEntity cutUpEntity = new CutUpEntity();
            cutUpEntity.setName(radarSplit.getSplitName());
            cutUpEntity.setCoalPileDensity(Double.parseDouble(radarSplit.getCoalPileDensity()));
            cutUpEntity.setCoalPileGround(Double.parseDouble(radarSplit.getCoalPileGround()));
            cutUpController.update(cutUpEntity);
            return toAjax(b);
        }catch (Exception e){
            return AjaxResult.success("操作失败");
        }
    }

    @DeleteMapping("/{splitIds}")
    @Transactional
    public AjaxResult delete(@PathVariable String[] splitIds)
    {
        try{
            ArrayList<String> ids = new ArrayList<>();
            Collections.addAll(ids, splitIds);
            boolean a = radarSplitService.removeByIds(ids);
            boolean b = radarSplitSurfaceService.removeByIds(ids);
//            Integer[] integers = Arrays.stream(splitIds).mapToInt(Integer::valueOf).boxed().toArray(Integer[]::new);
//            cutUpController.delete(integers);
            return toAjax(a&&b);
        }catch (Exception e){
            return AjaxResult.success("操作失败");
        }
    }


}
